/**
 *
 */
package com.ssyt.examinationsystem.dao.impl;

import com.jdbc.util.JdbcUtils;
import com.ssyt.examinationsystem.dao.ISubAnswerInfoDAO;
import com.ssyt.examinationsystem.entity.SubjectiveAnswerInfo;
import com.ssyt.examinationsystem.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
public class SubAnswerInfoDAOImpl implements ISubAnswerInfoDAO {

    @Override
    public int addSubAnswer(SubjectiveAnswerInfo subAnswer) {
        String sql = "insert into subjectiveAnswerInfo values(null,?,?,?,?)";
        Object[] parameters = {subAnswer.getAnswer(), subAnswer.getPaper().getPaperId(), subAnswer.getStu().getStuId(),
                subAnswer.getIds()};
        return JdbcUtils.executeUpdate(sql, parameters);
    }

    /*
     * (non-Javadoc)
     *
     * @see com.ssyt.examinationsystem.dao.ISubAnswerInfoDAO#getSubAnswer()
     */
    @Override
    public List<Map<String, Object>> getSubAnswer() {

        return JdbcUtils.executeQuery("select * from subjectiveAnswerInfo");
    }

    /*
     * (non-Javadoc)
     *
     * @see
     * com.ssyt.examinationsystem.dao.ISubAnswerInfoDAO#getSubAnswerBySubId(java
     * .lang.String)
     */
    @Override
    public Map<String, Object> getSubAnswerBySubId(String subId) {
        List<Map<String, Object>> list = JdbcUtils.executeQuery("select * from subjectiveAnswerInfo where subId=?",
                subId);
        return CollectionUtils.isEmptyForList(list) ? list.get(0) : null;
    }

    /* (non-Javadoc)
     * @see com.ssyt.examinationsystem.dao.ISubAnswerInfoDAO#getSubAnswerByPaperIdAndStuId(java.lang.String, java.lang.String)
     */
    @Override
    public String getSubAnswerByPaperIdAndStuId(String paperId,
                                                String stuId) {
        List<Map<String, Object>> list = JdbcUtils.executeQuery("select subId from subjectiveAnswerInfo where paperId=? and stuId=?", paperId, stuId);
        return CollectionUtils.isEmptyForList(list) ? String.valueOf(list.get(0).get("subId")) : "";
    }

    /* (non-Javadoc)
     * @see com.ssyt.examinationsystem.dao.ISubAnswerInfoDAO#getReCheckList(java.lang.String)
     */
    @Override
    public List<Map<String, Object>> getReCheckList(String subIds) {
        String sql = "select * from subjectiveAnswerInfo where subId in (" + subIds + ") order by field(subId,"
                + subIds + ")";

        return JdbcUtils.executeQuery(sql);
    }

}
